package com.huawei.appmarket.service.appdetail.view.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewConfiguration;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.OverScroller;
import androidx.viewpager.widget.ViewPager;
import com.huawei.hms.framework.network.restclient.hwhttp.route.IPRoute;
import java.lang.ref.WeakReference;
import o.drv;
import o.eqb;

public class NestScrollLayout extends LinearLayout {
    private static float ˏ = ((float) (Math.log(0.78d) / Math.log(0.9d)));
    private int ʻ;
    private boolean ʻॱ;
    private float ʼ;
    private int ʽ;
    private float ʽॱ = ViewConfiguration.getScrollFriction();
    private float ʿ;
    private d ˈ;
    private OverScroller ˊ;
    private ViewPager ˊॱ;
    private VelocityTracker ˋ;
    private LinearLayout ˋॱ;
    private int ˎ;
    private LinearLayout ˏॱ;
    private int ͺ;
    float ॱ;
    private int ॱˊ;
    private boolean ॱˋ;
    private boolean ॱˎ = false;
    private boolean ॱॱ;
    private a ॱᐝ;
    private boolean ᐝ = false;
    private boolean ᐝॱ = false;

    public interface a {
        boolean ʼᐝ();

        void ˋ(int i, int i2);
    }

    static class d extends Handler {
        WeakReference<drv> ˋ;
        private int ˎ = 0;
        WeakReference<View> ॱ;

        d(drv o_drv, View view) {
            this.ˋ = new WeakReference(o_drv);
            this.ॱ = new WeakReference(view);
        }

        public void handleMessage(Message message) {
            if (this.ॱ != null && this.ˋ != null) {
                View view = (View) this.ॱ.get();
                if (view != null && message.what == 1001) {
                    if (this.ˎ == view.getScrollY()) {
                        drv o_drv = (drv) this.ˋ.get();
                        if (o_drv != null) {
                            o_drv.ˋ(0);
                            return;
                        }
                        return;
                    }
                    sendMessageDelayed(obtainMessage(1001), 200);
                    this.ˎ = view.getScrollY();
                }
            }
        }
    }

    public NestScrollLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        setOrientation(1);
        this.ˊ = new OverScroller(context);
        this.ˎ = ViewConfiguration.get(context).getScaledTouchSlop();
        this.ʻ = ViewConfiguration.get(context).getScaledMaximumFlingVelocity();
        this.ʽ = ViewConfiguration.get(context).getScaledMinimumFlingVelocity();
        this.ʿ = ((context.getResources().getDisplayMetrics().density * 160.0f) * 386.0878f) * 0.84f;
    }

    public void setHeadView(LinearLayout linearLayout) {
        this.ˋॱ = linearLayout;
    }

    public void setViewPager(ViewPager viewPager) {
        this.ˊॱ = viewPager;
    }

    public void setNavigatorView(LinearLayout linearLayout) {
        this.ˏॱ = linearLayout;
    }

    private void ˋ() {
        if (this.ˋ == null) {
            this.ˋ = VelocityTracker.obtain();
        }
    }

    private void ॱ() {
        if (this.ˋ != null) {
            this.ˋ.recycle();
            this.ˋ = null;
        }
    }

    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        float y = motionEvent.getY();
        switch (action) {
            case 0:
                this.ʼ = y;
                this.ॱ = y;
                break;
            case 2:
                float f = y - this.ॱ;
                if (Math.abs(y - this.ʼ) > ((float) this.ˎ) && !this.ᐝ && ((ˎ() && this.ʻॱ && f > 0.0f) || (!(this.ʻॱ || this.ॱˋ) || ((this.ॱˋ && ˎ()) || (this.ॱˋ && f < 0.0f))))) {
                    this.ᐝ = true;
                    motionEvent.setAction(3);
                    MotionEvent obtain = MotionEvent.obtain(motionEvent);
                    dispatchTouchEvent(motionEvent);
                    obtain.setAction(0);
                    this.ॱ = y;
                    return dispatchTouchEvent(obtain);
                }
        }
        this.ॱ = y;
        return super.dispatchTouchEvent(motionEvent);
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        ˋ();
        this.ˋ.addMovement(motionEvent);
        int action = motionEvent.getAction();
        float y = motionEvent.getY();
        switch (action) {
            case 0:
                if (!this.ˊ.isFinished()) {
                    this.ˊ.abortAnimation();
                }
                this.ʼ = y;
                return true;
            case 1:
                if (this.ˈ != null) {
                    this.ˈ.sendMessageDelayed(this.ˈ.obtainMessage(1001), 5);
                }
                this.ॱॱ = false;
                this.ˋ.computeCurrentVelocity(1000, (float) this.ʻ);
                int yVelocity = (int) this.ˋ.getYVelocity();
                if (Math.abs(yVelocity) > this.ʽ) {
                    ˎ(yVelocity);
                }
                ॱ();
                break;
            case 2:
                float f = y - this.ʼ;
                if (!this.ॱॱ && Math.abs(f) > ((float) this.ˎ)) {
                    this.ॱॱ = true;
                }
                if (this.ॱॱ) {
                    scrollBy(0, (int) (-f));
                    if (ॱ(f)) {
                        motionEvent.setAction(0);
                        dispatchTouchEvent(motionEvent);
                        this.ᐝ = false;
                        this.ᐝॱ = true;
                        postDelayed(new Runnable(this) {
                            final /* synthetic */ NestScrollLayout ॱ;

                            {
                                this.ॱ = r1;
                            }

                            public void run() {
                                this.ॱ.ᐝॱ = false;
                            }
                        }, 50);
                    }
                }
                this.ʼ = y;
                break;
            case 3:
                this.ॱॱ = false;
                ॱ();
                if (!this.ˊ.isFinished()) {
                    this.ˊ.abortAnimation();
                    break;
                }
                break;
        }
        return super.onTouchEvent(motionEvent);
    }

    private void ˎ(int i) {
        int ˊ = (int) ˊ(i);
        if (i > 0) {
            if (this.ˊ.getCurrY() - ˊ <= 0) {
                ˊ(0, IPRoute.INTERVAL);
            } else {
                ˋ(-i);
            }
        }
        if (i >= 0) {
            return;
        }
        if ((this.ͺ - this.ˊ.getCurrY()) - ˊ <= 0) {
            ˊ(this.ͺ, IPRoute.INTERVAL);
        } else {
            ˋ(-i);
        }
    }

    private boolean ॱ(float f) {
        if (!this.ˊ.isFinished() || ((getScrollY() != this.ͺ || f >= 0.0f) && (getScrollY() != 0 || f <= 0.0f || ˎ()))) {
            return false;
        }
        return true;
    }

    private void ˊ(int i, int i2) {
        int scrollY = getScrollY();
        this.ˊ.startScroll(0, scrollY, 0, i - scrollY, i2);
        invalidate();
    }

    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        float y = motionEvent.getY();
        if (!ˏ(action)) {
            return true;
        }
        switch (action) {
            case 0:
                this.ʼ = y;
                break;
            case 2:
                float f = y - this.ʼ;
                if (Math.abs(f) > ((float) this.ˎ)) {
                    this.ॱॱ = true;
                    if (this.ॱˋ && !ˎ() && f > 0.0f && this.ˊ.isFinished()) {
                        this.ᐝ = false;
                        return false;
                    } else if (!this.ʻॱ) {
                        return true;
                    } else {
                        if (ˎ() && this.ʻॱ && f > 0.0f) {
                            return true;
                        }
                    }
                }
                break;
        }
        return super.onInterceptTouchEvent(motionEvent);
    }

    private boolean ˏ(int i) {
        if (!this.ᐝ && this.ᐝॱ && i == 1 && ˎ() && this.ʻॱ) {
            return false;
        }
        if (this.ˊ.isFinished() || !this.ᐝ) {
            return true;
        }
        return false;
    }

    protected void onScrollChanged(int i, int i2, int i3, int i4) {
        super.onScrollChanged(i, i2, i3, i4);
        if (this.ॱᐝ != null) {
            this.ॱᐝ.ˋ(getScrollY(), this.ॱˊ);
        }
    }

    public void setOnScrollListener(a aVar) {
        this.ॱᐝ = aVar;
    }

    public void setImmerse(boolean z) {
        this.ॱˎ = z;
    }

    private boolean ˎ() {
        if (this.ॱᐝ != null) {
            return this.ॱᐝ.ʼᐝ();
        }
        return false;
    }

    protected void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        getChildAt(0).measure(i, MeasureSpec.makeMeasureSpec(0, 0));
        if (getChildAt(1) != null) {
            getChildAt(1).measure(i, MeasureSpec.makeMeasureSpec(0, 0));
        }
        if (this.ˊॱ != null) {
            LayoutParams layoutParams = this.ˊॱ.getLayoutParams();
            this.ॱˊ = this.ˋॱ.getMeasuredHeight();
            this.ͺ = this.ॱˊ;
            if (this.ॱˎ) {
                layoutParams.height = (getMeasuredHeight() - this.ˏॱ.getMeasuredHeight()) - eqb.ॱ();
                this.ͺ = this.ॱˊ - eqb.ॱ();
            } else {
                layoutParams.height = getMeasuredHeight() - this.ˏॱ.getMeasuredHeight();
            }
            setMeasuredDimension(getMeasuredWidth(), (this.ˋॱ.getMeasuredHeight() + this.ˏॱ.getMeasuredHeight()) + this.ˊॱ.getMeasuredHeight());
        }
    }

    public void ˋ(int i) {
        this.ˊ.fling(0, getScrollY(), 0, i, 0, 0, 0, this.ͺ);
        invalidate();
    }

    public void scrollTo(int i, int i2) {
        boolean z = true;
        this.ʻॱ = false;
        if (i2 < 0) {
            this.ॱˋ = true;
            i2 = 0;
        }
        if (i2 > this.ͺ) {
            i2 = this.ͺ;
            this.ʻॱ = true;
            this.ॱˋ = false;
        }
        if (i2 != getScrollY()) {
            super.scrollTo(i, i2);
        }
        this.ॱˋ = getScrollY() == 0;
        if (getScrollY() != this.ͺ) {
            z = false;
        }
        this.ʻॱ = z;
        if ((this.ʻॱ || this.ॱˋ) && this.ˊ != null && !this.ˊ.isFinished()) {
            this.ˊ.abortAnimation();
        }
    }

    private double ॱ(int i) {
        return Math.log((double) ((0.35f * ((float) Math.abs(i))) / (this.ʽॱ * this.ʿ)));
    }

    private double ˊ(int i) {
        return Math.exp(ॱ(i) * (((double) ˏ) / (((double) ˏ) - 1.0d))) * ((double) (this.ʽॱ * this.ʿ));
    }

    public void computeScroll() {
        if (this.ˊ.computeScrollOffset()) {
            scrollTo(0, this.ˊ.getCurrY());
            invalidate();
        }
    }

    public void setStateListener(drv o_drv) {
        this.ˈ = new d(o_drv, this);
    }
}
